package com.keepers.repair.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.widget.Button;

import com.keepers.repair.R;
import com.keepers.repair.helper.ConsValue;
import com.keepers.repair.utils.AlertToast;
import com.keepers.repair.view.MovieRecorderView;
import com.keepers.repair.view.TitleBar;

/**
 * auth aboom
 * date 2018/11/25
 */
public class MovieActivity extends BaseActivity {

    private MovieRecorderView mRecorderView;//视频录制控件
    private Button            mShootBtn;//视频开始录制按钮
    private boolean           success;//防止录制完成后出现多次跳转事件


    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);
        TitleBar titleBar = f(R.id.title_bar);
        titleBar.initializeHeader("视频录制");
        mRecorderView = f(R.id.movie_recorder);
        mShootBtn = f(R.id.shoot_button);

        mShootBtn.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mShootBtn.setBackgroundResource(R.drawable.ic_bg_movie_add_shoot_select);
                    mRecorderView.record(() -> {
                        //判断用户按下时间是否大于10秒
                        if (!success && mRecorderView.getTimeCount() < 10) {
                            success = true;
                            movie();
                        }
                    });
                    break;
                case MotionEvent.ACTION_UP:
                    mShootBtn.setBackgroundResource(R.drawable.ic_bg_movie_add_shoot);
                    //判断用户按下时间是否大于3秒
                    if (mRecorderView.getTimeCount() > 3) {
                        if (!success) {
                            success = true;
                            movie();
                        }
                    } else {
                        success = false;
                        if (mRecorderView.getmVecordFile() != null) {
                            mRecorderView.getmVecordFile().delete();//删除录制的过短视频
                        }
                        mRecorderView.stop();//停止录制
                        AlertToast.show("视频录制时间太短");
                    }
                    break;
                default:
                    break;
            }
            return true;
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        success = false;
        mRecorderView.stop();//停止录制
    }

    public void movie() {
        if (success) {
            String filePath = mRecorderView.getmVecordFile().toString();
            mRecorderView.stop();
            success = false;
            Intent intent = new Intent(this, MovieSuccessActivity.class);
            intent.putExtra(ConsValue.INTENT_KEY, filePath);
            startActivityForResult(intent, 0);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            setResult(RESULT_OK, data);
            finish();
        }
    }
}
